package com.soxing.braindata.web.controller;

import com.soxing.braindata.common.config.FileConfig;
import com.soxing.braindata.common.util.FileUtil;
import com.soxing.common.utils.response.WebResponse;
import com.soxing.common.utils.util.EncryptUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @author :durango
 * @date :2022/7/19 14:09
 * @description 文件控制器
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Api(value = "文件管理", tags = {"文件管理"})
public class FileController {

    @Resource
    private FileConfig fileConfig;

    @PostMapping("/uploadFile")
    @ApiOperation(value = "文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "文件类型.实验规范:specification,标签含义:tag")
    })
    public WebResponse<String> uploadFile(HttpServletRequest request, MultipartFile file, @RequestParam String type) {
        String filePath = null;
        try {
            if (file == null || file.isEmpty()) {
                return WebResponse.fail("请上传文件");
            }
            File folder = new File(fileConfig.getAddress());
            if (!folder.exists()) {
                if (!folder.mkdirs()) {
                    return WebResponse.fail("创建文件夹失败");
                }
            }
            String fileName = file.getOriginalFilename();
            int index = fileName.lastIndexOf(".");
            String prefix = fileName.substring(0, index);
            String suffix = fileName.substring(index + 1);
            filePath = FileUtil.calcFilePath(fileConfig.getAddress() + FileUtil.SEPARATOR + type,
                    fileConfig.getMaxFiles(),
                    prefix + FileUtil.DOT + System.currentTimeMillis() + FileUtil.DOT + suffix);
            FileUtil.saveFile(filePath, file);
        } catch (Exception e) {
            log.error("上传文件异常", e);
            return WebResponse.fail("上传文件失败");
        }
        return WebResponse.success(filePath);
    }

    @ApiOperation(value = "文件下载")
    @RequestMapping("/downloadFile")
    public void download(HttpServletResponse response, @RequestParam String filePath) {
        try {
            //String realFilePath = EncryptUtil.decrypt(filePath, fileConfig.getSecretKey());
            File file = new File(filePath);
            if (!file.exists()) {
                log.error("文件{}不存在", file);
                return;
            }
            FileUtil.downloadFile(response, file);
        } catch (Exception e) {
            log.error("文件路径解密异常", e);
        }
    }
}
